package com.mDigitalHealth.activity;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.ComponentName;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.net.ConnectivityManager;
import android.os.Bundle;
import android.webkit.WebView;

import com.mDigitalHealth.R;

public class WebViewActivity extends Activity {

	private WebView webview = null;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.webview);

		// AndroidManifest.xml中必须使用许可"android.permission.INTERNET",否则会出Web page not available错误
		webview = (WebView) findViewById(R.id.webv);
		// 页面可以使用javascript
		webview.getSettings().setJavaScriptEnabled(true);
		webview.getSettings().setSupportZoom(true);
		Intent intent = getIntent();
		String urlcontent = intent.getStringExtra("urlcontent");
		// webview.loadUrl("http://www.google.com.hk");
		NetWorkStatus();

		webview.loadUrl("http://118.192.25.71/mobilelife");

		// if (URLUtil.isNetworkUrl(urlcontent)) {
		// webview.loadUrl(urlcontent);
		// } else {
		// System.out.println("url is error");
		// }
	}

	private boolean NetWorkStatus() {

		boolean netSataus = false;
		ConnectivityManager cwjManager = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);

		cwjManager.getActiveNetworkInfo();

		if (cwjManager.getActiveNetworkInfo() != null) {
			netSataus = cwjManager.getActiveNetworkInfo().isAvailable();
		}

		if (netSataus) {
			AlertDialog.Builder b = new AlertDialog.Builder(this).setTitle("没有可用的网络").setMessage(
					"是否对网络进行设置？");
			b.setPositiveButton("是", new DialogInterface.OnClickListener() {
				public void onClick(DialogInterface dialog, int whichButton) {
					Intent mIntent = new Intent("/");
					ComponentName comp = new ComponentName("com.android.settings",
							"com.android.settings.WirelessSettings");
					mIntent.setComponent(comp);
					mIntent.setAction("android.intent.action.VIEW");
					startActivityForResult(mIntent, 0); // 如果在设置完成后需要再次进行操作，可以重写操作代码，在这里不再重写

					// 重新加载
					webview.loadUrl("http://118.192.25.71/mobilelife");
				}
			}).setNeutralButton("否", new DialogInterface.OnClickListener() {
				public void onClick(DialogInterface dialog, int whichButton) {
					dialog.cancel();
				}
			}).show();
		}

		return netSataus;
	}
}
